package cn.antia.cwin;

import cn.antia.cwin.grid.SingleGrid;
import cn.antia.cwin.window.SingleWindow;
import cn.antia.cwin.window.MultiWindow;
import cn.antia.cep.common.AntiaLog;

import java.util.List;
import java.util.Map;

/**
 * User: Jacob Xiao
 * Date: 2008-7-18
 * Time: 13:55:48
 */
public class ConfigPoolHelper {
    /**
	 * 取通讯对象 (根据表名与字段名取通讯对象)
	 * @param table 表名
	 * @param field 字段名
	 * @return 通讯对象
     * @throws cn.antia.cep.common.AntiaException a*/
	public static Cele getElementComsat(String table,String field){
		List<Cele> elements = ConfigPool.getElementComsats(table);
        for (Cele cele:elements) {
            if (cele.getField().getFieldName().equalsIgnoreCase(field)) {
                return cele;
            }
        }
        AntiaLog.debug("取表" + table + " 字段 " + field + " 的通讯对象时出错。如果是附加字段表示并没有出错。");
		return null;
	}

	/**
	 * 取得通信对象集合
	 * @param table 表名
	 * @return 通信对象列表
	 * */
	public static List<Cele> getComsats(String table) {
		return ConfigPool.getElementComsats(table);
	}

    /**
	 * 取单表表格配置
	 * @param gridId gridId
	 * @return SingleGrid
	 */
	public static SingleGrid getSingleGrid(String gridId){
		return ConfigPool.getSingleGrid(gridId);
	}
	/**
	 * 取得单个WINDOW
	 * @param winId winId
	 * @return SingleWindow
	 */
	public static SingleWindow getSingleWindow(String winId){
        return ConfigPool.getSingleWindow(winId);
	}

	/**
	 * 取单个WINDOW的某个FIELD
	 * @param winId window id
	 * @param field field
	 * @return Cele
	 */
	public static Cele getSingleWindowField(String winId,String field){
		SingleWindow window = ConfigPool.getSingleWindow(winId);
		List<Cele> list = window.getFields();
        for (Cele ec : list) {
            if (ec.getField().getFieldName().equals(field)) {
                return ec;
            }
        }
		return null;
	}
	/**
	 * 取得多窗口WINDOW (不克隆对象)
	 * @param id id
	 * @return MultiWindow
	 */
	public  static MultiWindow getMultiWindow(String id){
        return ConfigPool.getMultiWindow(id);
	}

    public static Cele getCommonField(String field) {
        Map<String, Cele> map = ConfigPool.getCommonElements();
        if (map != null && map.size() > 0) {
            return map.get(field);
        }
        return null;
    }

    public static List<Cele> getDbElements(String table) {
        return ConfigPool.getDbElements(table);
    }

    public static Cele getDBCele(String table, String field) {
        List<Cele> lc = ConfigPool.getDbElements(table);
        for (Cele ce : lc) {
            if (field.equalsIgnoreCase(ce.getField().getFieldName())) {
                return ce;
            }
        }
        return null;
    }
}
